package search

/**
二分查找
*/
type SSeqBinSearch struct {
	sList []int
}

func NewSSeqBinSearch(list []int) *SSeqBinSearch {
	seq := &SSeqBinSearch{
	}

	seq.sList = list
	return seq
}

// 前提条件，数组是有序
func (seq *SSeqBinSearch) Search(value int) int {
	low, high := 0, len(seq.sList)-1

	for ; low <= high; {
		place := (low + high) / 2
		if value == seq.sList[place] {
			return place
		} else if value < seq.sList[place] {
			high = place - 1
		} else {
			low = place + 1
		}
	}

	return -1
}
